package com.automannn.atm_back.modules.codeGenerator.service.impl;

import com.automannn.atm_back.modules.codeGenerator.entity.ColumnConfigEntity;
import com.automannn.atm_back.modules.codeGenerator.entity.GlobalConfigEntity;
import com.automannn.atm_back.modules.codeGenerator.mapper.GlobalConfigEntityMapper;
import com.automannn.atm_back.modules.codeGenerator.service.GlobalConfigEntityService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (GeneratorGlobalConfig)表服务实现类
 *
 * @author chenkaihai
 * @since 2020-07-12 16:58:13
 */
@Service
public class GlobalConfigEntityServiceImpl implements GlobalConfigEntityService {
    @Resource
    private GlobalConfigEntityMapper globalConfigEntityMapper;


    /**
     * 查询一个对象
     *
     * @param id return 实例对象
     */
    @Override
    public GlobalConfigEntity queryOne(String id) {
        return this.globalConfigEntityMapper.queryById(id);
    }

    /**
     * 查询所有对象
     * <p>
     * return 实例对象
     */
    @Override
    public IPage<GlobalConfigEntity> queryAll(Page<GlobalConfigEntity> page, GlobalConfigEntity generatorGlobalConfig) {
        return this.globalConfigEntityMapper.queryAll(page, generatorGlobalConfig);
    }

    /**
     * 新增数据
     *
     * @param generatorGlobalConfig 实例对象
     * @return 实例对象
     */
    @Override
    public GlobalConfigEntity insert(GlobalConfigEntity generatorGlobalConfig) {
        this.globalConfigEntityMapper.insert(generatorGlobalConfig);
        return generatorGlobalConfig;
    }

    /**
     * 修改数据
     *
     * @param generatorGlobalConfig 实例对象
     * @return 实例对象
     */
    @Override
    public void update(GlobalConfigEntity generatorGlobalConfig) {
        globalConfigEntityMapper.update(generatorGlobalConfig);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.globalConfigEntityMapper.deleteById(id) > 0;
    }

    @Override
    public void deleteBatchByIds(String ids) {
        globalConfigEntityMapper.deleteBatchIds(Arrays.asList(ids.split(",")));
    }

    @Override
    public void saveOrUpdate(List<GlobalConfigEntity> globalConfigEntityList) {
        List<GlobalConfigEntity>   toUpdateList=
                globalConfigEntityList.stream().filter(globalConfigEntity -> globalConfigEntity.getId() != null && globalConfigEntity.getId() != "").collect(Collectors.toList());
        List<GlobalConfigEntity> toAddList =
                globalConfigEntityList.stream().filter(globalConfigEntity -> globalConfigEntity.getId() == null || globalConfigEntity.getId() == "").collect(Collectors.toList());
        toAddList.forEach(globalConfigEntity -> globalConfigEntityMapper.insert(globalConfigEntity));
        toUpdateList.forEach(
                globalConfigEntity -> globalConfigEntityMapper.updateById(globalConfigEntity));

    }
}
